//use gloo::console::log;
use crate::errors::{MyError, MyResult};
use crate::utils::helper::{get_document, get_window};
use wasm_bindgen::JsCast;
use web_sys::{Document, Element, HtmlSpanElement, Node, Selection};

pub fn get_selection() -> MyResult<Selection> {
    let window = get_window();
    window
        .get_selection()
        .map_err(|err| MyError::custom(&format!("get_selection error:{:?}", err.as_string())))?
        .ok_or_else(|| MyError::custom("get_selection None"))
}
pub fn create_span(document: &Document) -> HtmlSpanElement {
    document
        .create_element("span")
        .expect("apply_title/create_element error")
        .dyn_into::<HtmlSpanElement>()
        .expect("apply_title/dyn_into error")
}
pub fn get_win_inner_height() -> MyResult<f64> {
    get_window()
        .inner_height()
        .map_err(|err| {
            MyError::custom(&format!("get_win_inner_height error:{:?}", err.as_string()))
        })?
        .as_f64()
        .ok_or_else(|| MyError::custom("get_win_inner_height/as_f64 None"))
}
pub fn get_win_inner_width() -> MyResult<f64> {
    get_window()
        .inner_width()
        .map_err(|err| {
            MyError::custom(&format!("get_win_inner_width error:{:?}", err.as_string()))
        })?
        .as_f64()
        .ok_or_else(|| MyError::custom("get_win_inner_width/as_f64 None"))
}

pub fn get_element_by_id(id: &str) -> MyResult<Element> {
    get_document()
        .get_element_by_id(id)
        .ok_or_else(|| MyError::custom(&format!("get_element_by_id error. Input id:{}", id)))
}

pub fn get_first_child(node: &Node) -> MyResult<Node> {
    node.first_child()
        .ok_or_else(|| MyError::custom("first_child is None"))
}

pub trait ElementHelper {}

impl ElementHelper for Element {}
